﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExtAspNet;
using MyDMS.DAL;
using ListItem = ExtAspNet.ListItem;

namespace MyDMS_ExtJS.Admin
{
    public partial class UserEdit : System.Web.UI.Page
    {
        private int _User_Code = 0;
        MyDMSEntity _myDmsEntity = new MyDMSEntity();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["id"] != null)
            {
                _User_Code = Convert.ToInt32(Request.QueryString["id"]);
            }
            if (!IsPostBack)
            {
                BindDepartment();
                BindRole();
                var sf = _myDmsEntity.User.FirstOrDefault(s => s.User_Code == _User_Code);
                if (sf != null)
                {
                    var firstOrDefault =
                        _myDmsEntity.Department.FirstOrDefault(dp => dp.Department_Code == sf.Department_Code);
                    if (firstOrDefault != null)
                    {
                        var departmentName =
                            firstOrDefault.Department_Name;
                        if (sf != null)
                        {
                            tbUserName.Text = sf.User_Name;
                            tbPassword.Text = sf.User_Password;
                            tbFirstName.Text = sf.FirstName;
                            tbLastName.Text = sf.LastName;
                            cbIsActive.Checked = sf.Is_Active;
                            ddlDepartmentName.SelectedValue = sf.Department_Code.ToString();
                        }
                    }
                }
            }
        }
        private void BindRole()
        {
            ddlRole.DataSource = _myDmsEntity.UserRole.ToList();
            ddlRole.DataTextField = "Role_Name";
            ddlRole.DataValueField = "Role_Code";
            ddlRole.DataBind();
        }
        private void BindDepartment()
        {
            ddlDepartmentName.DataSource = _myDmsEntity.Department.ToList();
            ddlDepartmentName.DataValueField = "Department_Code";
            ddlDepartmentName.DataTextField = "Department_Name";
            ddlDepartmentName.DataBind();
            ddlDepartmentName.Items.Insert(0, new ListItem("选择科室", "-1"));
        }
        protected void btnSaveRefresh_Click(object sender, EventArgs e)
        {
            if (_User_Code != 0)
            {
                var sf = _myDmsEntity.User.FirstOrDefault(s => s.User_Code == _User_Code);
                if (sf != null)
                {
                    sf.User_Name = tbUserName.Text;
                    sf.User_Password = tbPassword.Text;
                    sf.FirstName = tbFirstName.Text;
                    sf.LastName = tbLastName.Text;
                    sf.Is_Active = cbIsActive.Checked;
                    sf.Department_Code = int.Parse(ddlDepartmentName.SelectedValue);
                }
                var uir = _myDmsEntity.UserInRole.FirstOrDefault(s => s.User_Code == _User_Code);
                if(uir!=null)
                {
                    uir.Role_Code = Convert.ToInt32(ddlRole.SelectedValue);
                }
                _myDmsEntity.SaveChanges();
            }
            else
            {
                var newuser = new MyDMS.DAL.User(){
                    User_Name = tbUserName.Text,
                    User_Password = tbPassword.Text,
                    FirstName = tbFirstName.Text,
                    LastName = tbLastName.Text,
                    Is_Active = cbIsActive.Checked,
                    Department_Code = int.Parse(ddlDepartmentName.SelectedValue),
                    Creation_Date = DateTime.Now,
                    Updation_Date = DateTime.Now,
                    Online_Status = ""
                };
                _myDmsEntity.User.AddObject(newuser);
                
                _myDmsEntity.SaveChanges();
                _myDmsEntity.UserInRole.AddObject(new MyDMS.DAL.UserInRole()
                                                      {
                                                          Creation_Date = DateTime.Now,
                                                          Role_Code = Convert.ToInt32(ddlRole.SelectedValue),
                                                          Updation_Date = DateTime.Now,
                                                          User_Code = newuser.User_Code
                                                      });
                _myDmsEntity.SaveChanges();
            }

            // 2. 关闭本窗体，然后刷新父窗体
            PageContext.RegisterStartupScript(ActiveWindow.GetHideRefreshReference());
        }
    }
}